/*
 * Copyright (C) 2019 ~ 2021 UnionTech Software Technology Co., Ltd.
 *
 * Author:     caitao <caitao@uniontech.com>
 *
 * Maintainer: caitao <caitao@uniontech.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "dlistviewmulticol.h"
#include "dlistviewmulticoldelegate.h"
#include "dlistviewmulticolcommon.h"

#include <QStandardItem>
#include <QDebug>
#include <QMenu>
#include <QScrollBar>
#include <QContextMenuEvent>
#include <QHeaderView>

DListViewMultiCol::DListViewMultiCol(QStandardItemModel *model, QHeaderView *headerView, QWidget *parent)
    : DListView(parent)
    , m_model(model)
    , m_headerView(headerView)
{
    initUi();
    initConnection();
}

DListViewMultiCol::~DListViewMultiCol()
{
}

void DListViewMultiCol::resizeEvent(QResizeEvent *event)
{
    Q_UNUSED(event);
    // 调整表头高度
    m_headerView->setFixedHeight(m_headerView->sizeHint().height());

    // 当窗口大小改变时，使表头最右侧栏拓展到窗口最右侧
    int rightestHeaderIndex = m_model->columnCount() - 1;
    m_headerView->resizeSection(
        rightestHeaderIndex, size().width() - (m_headerView->length() - m_headerView->sectionSize(rightestHeaderIndex)));
}

void DListViewMultiCol::updateGeometries()
{
    // 使滚动条适应界面
    resizeContents(m_headerView->length(), contentsSize().height());
    DListView::updateGeometries();

    update();
}

void DListViewMultiCol::contextMenuEvent(QContextMenuEvent *event)
{
    QMenu *menu = new QMenu(this);
    menu->move(event->pos().x(), event->pos().y());
    QAction *modifyAction = new QAction("modify", this);
    QAction *enableAction = new QAction("enable", this);
    QAction *disableAction = new QAction("disable", this);
    QAction *deleteAction = new QAction("delete", this);
    QList<QAction *> actionList;
    actionList << modifyAction << enableAction << disableAction << deleteAction;

    menu->addActions(actionList);
    menu->exec(QCursor::pos());
    menu->deleteLater();
}

// 当表头各列宽度改变
void DListViewMultiCol::onSectionResized(int logicalIndex, int oldSize, int newSize)
{
    Q_UNUSED(logicalIndex);
    Q_UNUSED(oldSize);
    Q_UNUSED(newSize);
    QList<int> headerWidthList;

    for (int i = 0; i < m_model->columnCount(); ++i) {
        headerWidthList.append(m_headerView->sectionSize(i));
    }

    m_model->setProperty(PROPERTY_KEY_HEADER_SECTION_WIDTH_LIST, QVariant::fromValue<QList<int>>(headerWidthList));

    // 调整表头宽度
    if (m_headerView->length() != m_headerView->width()) {
        qDebug() << "headview width";
        qDebug() << m_headerView->width();
        qDebug() << m_headerView->length();

        qDebug() << headerWidthList;
        m_headerView->setFixedWidth(m_headerView->length());
    }

    // 使滚动条适应界面
    updateGeometries();
}

// 当水平滑动条变动
void DListViewMultiCol::onHorizontalScrollBarValueChanged(int value)
{
    m_headerView->move(-value, 0);
}

// 当排序改变
void DListViewMultiCol::onHeadViewSortIndicatorChanged(int index, Qt::SortOrder order)
{
    m_model->sort(index, order);
    // 使滚动条适应界面
    updateGeometries();
}

void DListViewMultiCol::initUi()
{
    setModel(m_model);
    // ListMode
    setViewMode(ViewMode::ListMode);
    setItemDelegate(new DListViewMultiColDelegate(this));
    setOrientation(QListView::TopToBottom, false);
    setTextElideMode(Qt::ElideMiddle);
    setSelectionRectVisible(true);
    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    setUniformItemSizes(true);
    setResizeMode(Fixed);
    setSelectionMode(QAbstractItemView::SelectionMode::ExtendedSelection);
    setEditTriggers(DListView::EditTrigger::NoEditTriggers);

    // headerView
    // set header view properties
    m_headerView->setModel(Q_NULLPTR);
    m_headerView->setModel(m_model);

    //    m_headerView->setStretchLastSection(true);
    //    m_headerView->setHighlightSections(false);
    //    m_headerView->setSectionsClickable(true);
    //    m_headerView->setSortIndicatorShown(true);
    //    m_headerView->setDefaultAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    //    m_headerView->setContextMenuPolicy(Qt::CustomContextMenu);
    //    // 表头各部分可移动
    //    m_headerView->setSectionsMovable(true);

    //    m_headerView->setSectionResizeMode(QHeaderView::Interactive);
    //    m_headerView->setMinimumSectionSize(30);

    addHeaderWidget(m_headerView);
}

void DListViewMultiCol::initConnection()
{
    // header view connection
    // 表头各列宽度改变
    connect(m_headerView, &QHeaderView::sectionResized, this, &DListViewMultiCol::onSectionResized);
    // 水平滑动条变动
    connect(horizontalScrollBar(), &QScrollBar::valueChanged, this, &DListViewMultiCol::onHorizontalScrollBarValueChanged);
    // 排序改变
    connect(m_headerView, &QHeaderView::sortIndicatorChanged, this, &DListViewMultiCol::onHeadViewSortIndicatorChanged);
}
